<!DOCTYPE html>
<title>Test media element's "played" attribute and ranges.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="video-played.js"></script>
<video></video>
<script>
var video;
async_test(function(t) {
    var expectedStartTimes = [];
    var expectedEndTimes = [];
    video = document.querySelector("video");

    video.oncanplay = t.step_func(function() {
        video.oncanplay = null;
        testRanges(expectedStartTimes, expectedEndTimes);
        // Test playing when there are no ranges.
        timeRangeCount = currentTimeRange = 0;
        startPlayingInNewRange(t, expectedStartTimes);
    });
    waitForPauseAndContinue(t, jumpAndPlayFwd, false, expectedStartTimes, expectedEndTimes);

    function jumpAndPlayFwd() {
        // Test jumping forward into a new range and play.
        video.currentTime = (video.duration - 1.0).toFixed(2);
        currentTimeRange = 1;
        startPlayingInNewRange(t, expectedStartTimes);
        waitForPauseAndContinue(t, jumpBackAndPlayToNewRange, false, expectedStartTimes, expectedEndTimes);
    }

    function jumpBackAndPlayToNewRange() {
        // Test jumping backwards into a new range and play, should insert new range.
        video.currentTime = 3.0;
        startPlayingInNewRange(t, expectedStartTimes);
        waitForPauseAndContinue(t, jumpAndExtendRangeStart, false, expectedStartTimes, expectedEndTimes);
    }

    function jumpAndExtendRangeStart() {
        // Test playing into an existing range, should extend range start.
        var newTime = (video.played.start(currentTimeRange) - 0.05).toFixed(2);
        video.currentTime = newTime;
        expectedStartTimes[currentTimeRange] = newTime;
        playForDuration(0.1, t);
        waitForPauseAndContinue(t, jumpAndExtendRangeEnd, true, expectedStartTimes, expectedEndTimes);
    }

    function jumpAndExtendRangeEnd() {
        // Test jumping into an existing range and play beyond end, should extend range end.
        var newTime = (video.played.end(currentTimeRange) - 0.05).toFixed(2);
        video.currentTime = newTime;
        playForDuration(0.03, t);
        waitForPauseAndContinue(t, null, true, expectedStartTimes, expectedEndTimes);
    }

    video.src = "content/test.ogv";
});
</script>